package com.heima.wemedia.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.minio.MinIOTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.WmMaterialDto;
import com.heima.model.wemedia.entity.WmMaterial;
import com.heima.wemedia.dao.WmMaterialDao;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.utils.WmThreadLocalUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * 自媒体素材表(WmMaterial)表服务实现类
 *
 * @author makejava
 * @since 2022-04-12 18:09:39
 */
@Service("wmMaterialService")
public class WmMaterialServiceImpl implements WmMaterialService {
    @Resource
    private WmMaterialDao wmMaterialDao;

    @Resource
    private MinIOTemplate minIOTemplate;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WmMaterial queryById(Long id) {
        return this.wmMaterialDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param wmMaterial 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    /*@Override
    public Page<WmMaterial> queryByPage(WmMaterial wmMaterial, PageRequest pageRequest) {
        long total = this.wmMaterialDao.count(wmMaterial);
        return new PageImpl<>(this.wmMaterialDao.queryAllByLimit(wmMaterial, pageRequest), pageRequest, total);
    }*/

    /**
     * 新增数据
     *
     * @param wmMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public WmMaterial insert(WmMaterial wmMaterial) {
        this.wmMaterialDao.insert(wmMaterial);
        return wmMaterial;
    }

    /**
     * 修改数据
     *
     * @param wmMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public WmMaterial update(WmMaterial wmMaterial) {
        this.wmMaterialDao.update(wmMaterial);
        return this.queryById(wmMaterial.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.wmMaterialDao.deleteById(id) > 0;
    }

    @Override
    public ResponseResult upload(MultipartFile multipartFile) {
        //1.校验参数
        if (multipartFile == null || multipartFile.getSize() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.上传图片到Minion
        String fileName = multipartFile.getOriginalFilename(); //1.jpg
        //2.1生成唯一文件名称 afewaf.jpg
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID().toString().replace("-", "") + suffix;

        String url = null;
        try {
            url = minIOTemplate.uploadImgFile("", fileName, multipartFile.getInputStream());
        } catch (IOException e) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "素材上传失败");
        }

        //3.保存素材到
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.get().getId());
        wmMaterial.setUrl(url);
        wmMaterial.setType(0);
        wmMaterial.setIsCollection("0");
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setUpdatedTime(new Date());

        wmMaterialDao.insert(wmMaterial);
        //4.封装数据返回

        return ResponseResult.okResult(wmMaterial);
    }


    @Override
    public ResponseResult queryByPage(WmMaterialDto dto) {
        //1.校验参数
        dto.checkParam();
        //2.封装查询条件
        //2.1开启分页查询
        PageHelper.startPage(dto.getPage(), dto.getSize());
        //2.2执行查询

        WmMaterial queryParam = new WmMaterial();
        queryParam.setUserId(WmThreadLocalUtil.get().getId());
        if (dto.getIsCollection() != null && dto.getIsCollection() == 1) {
            queryParam.setIsCollection(dto.getIsCollection().toString());
        }
        //3.执行分页查询
        Page<WmMaterial> page = wmMaterialDao.queryall(queryParam);
        //4.封装数据返回
        ResponseResult result = new PageResponseResult(page.getPageNum(), page.getPageSize(), (int) page.getTotal());
        result.setData(page.getResult());
        return result;
    }

    /**
     * 测试uudi
     *
     * @param args
     */
    public static void main(String[] args) {
        /*String s=UUID.randomUUID().toString();
        System.out.println("s = " + s);*/

        String fileName = "1.jpg";
        //2.1生成唯一文件名称 afewaf.jpg
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID().toString().replace("-", "") + suffix;
        System.out.println("fileName = " + fileName);
    }
}
